package com.example.zzpneo4j.config;

import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.xmlpull.v1.XmlPullParserException;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * minio 核心配置类
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(MinioProp.class)
public class MinioConfig {

    @Resource
    private MinioProp minioProp;

    /**
     * 获取 MinioClient
     *
     * @return
     * @throws InvalidPortException
     * @throws InvalidEndpointException
     */
    @Bean
    public MinioClient minioClient() throws InvalidPortException,
            InvalidEndpointException, InvalidBucketNameException,
            InsufficientDataException, XmlPullParserException,
            ErrorResponseException, NoSuchAlgorithmException,
            IOException, NoResponseException,
            InvalidKeyException, InvalidResponseException,
            InternalException, RegionConflictException {
        MinioClient minioClient = new MinioClient(minioProp.getEndpoint(), minioProp.getAccessKey(), minioProp.getSecretKey());
        boolean bucketExists = minioClient.bucketExists(minioProp.getBucket());
        if (bucketExists) {
            log.info("--------------->{}", minioProp.getBucket() + "已存在");
        } else {
            minioClient.makeBucket(minioProp.getBucket());
        }
        return minioClient;
    }
}

